package niord.apps.activity;

import java.util.ArrayList;

import niord.apps.Product;
import niord.apps.adapters.ContactListAdapter;
import niord.apps.adapters.ExpandableAdapter;
import niord.apps.database.DataBase;
import niord.apps.helpers.Fonts;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AutoCompleteTextView;
import android.widget.ExpandableListView;

import com.niord.R;

public class MainActivity extends Activity implements OnKeyListener

{
	private ExpandableListView elv;
	private ExpandableAdapter eadapter;

	private AutoCompleteTextView actv;
	private ContactListAdapter cladapter;

	private DataBase db;

	private Fonts fonts;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		db = new DataBase(this);
		setContentView(R.layout.main);
		Fonts.initialize(this);
		db.restoreData();
		initExpListView();
		initClTextView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		db.close();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		db.saveData(eadapter.getList());
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		db.saveData(eadapter.getList());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		db.restoreData();
		eadapter.updateList();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		db.restoreData();
		eadapter.updateList();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);

		if (eadapter != null)
			eadapter.notifyDataSetChanged();
	}

	// *******************************************//
	// Code to make expandableListView
	// *******************************************//
	private void initExpListView(ArrayList<Product> persons) {
		elv = (ExpandableListView) findViewById(R.id.PersonListView);
		elv.setGroupIndicator(null);
		elv.setDivider(getResources().getDrawable(R.color.light_grey));
		elv.setChildDivider(getResources().getDrawable(R.color.solid_white));
		elv.setDividerHeight(1);

		eadapter = new ExpandableAdapter(this, persons);
		elv.setAdapter(eadapter);

	}

	// *******************************************//
	// Code to make contact autocomplete textView
	// *******************************************//
	private void initClTextView() {
		ContentResolver content = getContentResolver();
		Cursor cursor = content.query(Contacts.CONTENT_URI,
				ContactListAdapter.CONTACT_PROJECTION, null, null, null);
		cladapter = new ContactListAdapter(this, cursor);
		actv = (AutoCompleteTextView) findViewById(R.id.NameField);
		actv.setAdapter(cladapter);
		actv.setOnKeyListener(this);
		actv.setTypeface(fonts.hattori);

	}

	// #region Event handlers to realize text adding to ListView
	// public void onClick(View v) {
	// // sendText();
	// }

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER:
				sendText();
				return true;
			}
		}
		return false;
	}

	// Add text to ListView
	private void sendText() {
		String text = actv.getText().toString().trim();

		if ((eadapter.getNames().add(text)) && (!text.isEmpty())) {
			eadapter.addPerson(new Product(text));
			actv.setText("");
		}
	}

}